from module import db,table,templates

class Page(object):
    """Klasse (Abstract) zum Erstellen der jeweiligen Webseiten,
       nach dem Template-System"""
       
    TABLE_HEADERS_C = ["firstname","lastname","address1","city","state","zip","country","email","phone","username","age","gender"]
    TABLE_HEADERS_P = ["produkt_id","Kategorie","title","actor","price","special","common_prod_id"]
    TABLE_HEADERS_O = ["order_od", "orderdate","customerid","netamount","tax","totalmount"]
    
    def __init__(self,template=None):
        self.tpl = templates.Template()
        if template is not None:
            self.template = templates.read_template(template)
        else:
            self.template = self.fill_customer_view_table()
        self.dic = {}
    
    def render(self):
        self.dic['content'] = self.template
        return self.tpl.master % self.dic
    
    
    def fill_customer_view_table(self):
        table_tpl = self.tpl.tableview
        tab = table.Table(Page.TABLE_HEADERS_C)
        table_dic = {}
        lis = []
        
        for row in db.getCustomers():
            if not lis:
                lis.append(tab.tline%''.join(map(lambda k:tab.thead%k,tab.table_headers)))
            lis.append(tab.tline%''.join(map(lambda k:tab.tdata%k,row)))
        
        table_dic['table_content'] = ''.join(lis)
        table_dic['page_number'] = 1
        table_dic['amount_number'] = 20
        
        return table_tpl % table_dic
    
    def fill_product_view_table(self):
        table_tpl = self.tpl.tableview
        tab = table.Table(Page.TABLE_HEADERS_P)
        table_dic = {}
        lis = []
        
        for row in db.getProducts():
            if not lis:
                lis.append(tab.tline%''.join(map(lambda k:tab.thead%k,tab.table_headers)))
            lis.append(tab.tline%''.join(map(lambda k:tab.tdata%k,row)))
        
        table_dic['table_content'] = ''.join(lis)
        table_dic['page_number'] = 1
        table_dic['amount_number'] = 20
        
        return table_tpl % table_dic
    
    def fill_order_view_table(self):
        table_tpl = self.tpl.tableview
        tab = table.Table(Page.TABLE_HEADERS_O)
        table_dic = {}
        lis = []
        
        for row in db.getOrders():
            if not lis:
                lis.append(tab.tline%''.join(map(lambda k:tab.thead%k,tab.table_headers)))
            lis.append(tab.tline%''.join(map(lambda k:tab.tdata%k,row)))
        
        table_dic['table_content'] = ''.join(lis)
        table_dic['page_number'] = 1
        table_dic['amount_number'] = 20
        
        return table_tpl % table_dic
    
    def fill_order_cat(self):
        order_tpl = self.tpl.orderview
        option = "<option> %s </option>"
        lis = list()
        order_dic = {}
        
        for opt in db.getCategories():
            lis.append(option%opt)
        order_dic['categories'] = ''.join(lis)
        
        lis = list()
        for opt in db.getProductsName():
            lis.append(option%opt)
        order_dic['products'] = ''.join(lis)
        
        return order_tpl % order_dic
    
    
    def __iter__(self):
        for attr in dir(Page):
            if not attr.startswith("__"):
                yield attr
    
    def __call__(self):
        return self
        
    